package com.bus.core.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.bus.common.hibernate4.Updater;
import com.bus.common.jpa.JpaBaseDao;
import com.bus.core.dao.BusSiteDao;
import com.bus.core.entity.BusSite;

@CacheConfig(cacheNames="BusSite")
@Repository
public class BusSiteDaoImpl extends JpaBaseDao<BusSite, Integer> implements
		BusSiteDao {

	public int siteCount(boolean cacheable) {
		String hql = "select count(*) from BusSite bean";
		return ((Number) getEntityManager().createQuery(hql).getSingleResult())
				.intValue();
	}

	@SuppressWarnings("unchecked")
	@Cacheable
	public List<BusSite> getList(boolean cacheable) {
		String hql = "from BusSite bean order by bean.id asc";
		return getEntityManager().createQuery(hql).getResultList();
	}

	public int getCountByProperty(String property) {
		String hql = "select count(distinct " + property
				+ ") from BusSite bean ";
		Query query = getEntityManager().createQuery(hql);
		return ((Number) query.getSingleResult()).intValue();
	}

	@Cacheable
	public BusSite findById(Integer id) {
		BusSite entity = get(id);
		return entity;
	}

	@CacheEvict
	public BusSite save(BusSite bean) {
		getEntityManager().persist(bean);
		return bean;
	}

	@CacheEvict
	public BusSite deleteById(Integer id) {
		BusSite entity = super.get(id);
		if (entity != null) {
			getEntityManager().remove(entity);
		}
		return entity;
	}

	@Override
	protected Class<BusSite> getEntityClass() {
		return BusSite.class;
	}

	@Override
	public BusSite findByDomain(String domain) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BusSite updateByUpdater(Updater<BusSite> updater) {
		// TODO Auto-generated method stub
		return null;
	}
}